package yxy.game.pm2.bean.db.newDao;


import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.KowRecordPo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liangjianbin
 * @date 2022/3/1 8:40
 **/
public class KowRecordDAO {

    private static final String
            REPLACE_ONE = "replace into `kowRecord` () " +
            "values ()",
            SELECT_ALL = "select * from `kowRecord` where `pCode` = ?",
            DELETE = "delete from `kowRecord` where `pCode` = ? and `updateTime` != ?";

    public static List<KowRecordPo> getList(String playerCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{playerCode}, KowRecordPo.builder);
    }

    public static boolean replace(List<KowRecordPo> kowRecordPoList) {
        System.out.println("数据迁移开始");
        List<Object[]> list = new ArrayList<>();
        for (KowRecordPo po : kowRecordPoList) {
            Object[] objects = new Object[]{};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        System.out.println("数据迁移完成：" + kowRecordPoList.size() + ":" + ints.length);
        return ints.length == kowRecordPoList.size();
    }

    public static void delete(String pCode, long updateTime) {
        System.out.println("数据删除开始");
        int i = DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});
        System.out.println("数据删除完成:" + i);

    }

    public static KowRecordPo findOne(String playerCode) {
        String sql = "select * from `kowRecord` where `pCode` = ? ";
        List<KowRecordPo> list = DbManager.instance().executeQueryObjectList(sql, new Object[]{playerCode}, KowRecordPo.builder);
        return (list != null && list.size() > 0) ? list.get(0) : null;
    }
}
